﻿using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using AEM.Utils;

namespace AEM.Data.DAL
{
    public class FaultDefinition
    {
        public static BLL.Actions.FaultDefinition.AddFaultResult Add(Model.FaultDefinition model)
        {
            string condition = @"INSERT INTO [TableFaultDefinition] ([FaultCode], [FaultName], [FaultReason]) 
                                VALUES (@FaultCode, @FaultName, @FaultReason)";

            int ret = AccessHelper.ExecuteNonQuery(condition, Param(model));
            if (ret > 0) return BLL.Actions.FaultDefinition.AddFaultResult.OK;
            return BLL.Actions.FaultDefinition.AddFaultResult.FaultIsNull;
        }

        public static BLL.Actions.FaultDefinition.ModifiyFaultResult Update(Model.FaultDefinition model)
        {
            string condition = @"UPDATE [TableFaultDefinition] SET 
                                [FaultCode] = @FaultCode, 
                                [FaultName] = @FaultName, 
                                [FaultReason] = @FaultReason 
                                WHERE (([ID] = @ID))";

            int ret = AccessHelper.ExecuteNonQuery(condition, Param(model));
            if (ret > 0) return BLL.Actions.FaultDefinition.ModifiyFaultResult.OK;
            return BLL.Actions.FaultDefinition.ModifiyFaultResult.FaultIsNull;
        }

        public static bool Delete(int id)
        {
            return Global.Delete("TableFaultDefinition", "ID", id.ToString());
        }

        private static OleDbParameter[] Param(Model.FaultDefinition model)
        {
            List<OleDbParameter> param = new List<OleDbParameter>();
            param.Add(new OleDbParameter("@FaultCode", model.FaultCode));
            param.Add(new OleDbParameter("@FaultName", model.FaultName));
            param.Add(new OleDbParameter("@FaultReason", model.FaultReason));
            param.Add(new OleDbParameter("@ID", model.ID));
            return param.ToArray();
        }

        public static Model.FaultDefinition Select(int id)
        {
            return Select("ID", id.ToString());
        }

        public static Model.FaultDefinition Select(string field, string value)
        {
            string condition = " " + field + "={0}";
            if (value.isNumeric())
                condition = string.Format(condition, value);
            else
                condition = string.Format(condition, "'" + value + "'");
            DataTable dt = Global.List("TableFaultDefinition", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt.Rows[0]);
            }
            return null;
        }

        private static Model.FaultDefinition Deserialize(DataRow dr)
        {
            Model.FaultDefinition model = null;
            if (null != dr)
            {
                model = new Model.FaultDefinition();
                model.ID = dr[""].ToString().toInt32(); ;
                model.FaultCode = dr["FaultCode"].ToString();
                model.FaultName = dr["FaultName"].ToString();
                model.FaultReason = dr["FaultReason"].ToString();
            }
            return model;
        }

        public static List<Model.FaultDefinition> List(string condition)
        {
            DataTable dt = Global.List("TableFaultDefinition", condition);
            if (null != dt && dt.Rows.Count > 0)
            {
                return Deserialize(dt);
            }
            return null;
        }

        private static List<Model.FaultDefinition> Deserialize(DataTable dt)
        {
            List<Model.FaultDefinition> list = null;
            if (null != dt && dt.Rows.Count > 0)
            {
                list = new List<Model.FaultDefinition>();
                foreach (DataRow r in dt.Rows)
                {
                    list.Add(Deserialize(r));
                }
            }
            return list;
        }
    }
}
